package com.eryos.android.cigarettecounter.io;

import android.content.SharedPreferences;
import android.util.Log;

/**
 * Project :  CigaretteCounter
 * Author :   Olivier Combe 
 * URL :      http://eryos.fr/CigaretteCounter/
 *
 * Copyright (C) 2011 - Olivier Combe 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 *
 */

public class Preferences {

	public final static String LOG_TAG = "Preferences";
	
	public final static String PREF_SCREEN_WIDTH 	= "screenWidth";
	public final static String PREF_SCREEN_HEIGHT 	= "screenHeight";
	public final static String PREF_BOX_SIZE 		= "boxSize";
	public final static String PREF_BOX_PRICE 		= "boxPrice";
	public final static String PREF_CIGARETTE_PRICE = "cigarettePrice";
	public final static String PREF_TEST_UNLOCK		= "LOCKED";
	public final static String DATA_DAILY_COUNT		= "COUNT";
	
	public final static String WIDGET_ID_TAB		= "WIDGET_ID_TAB";

	public final static String MEDIUM_USAGE_LOW 	= "MEDIUM_USAGE_LOW";
	public final static String MEDIUM_USAGE_HIGH	= "MEDIUM_USAGE_HIGH";

	private int boxSize = -1;		// Quantity of cigarettes in a box
	private float boxPrice = -1f;	// Price of the box
	private float cigarettePrice = 0f;//boxPrice / boxSize;

	private int screenWidth = 0;
	private int screenHeight = 0;
	

	private int mediumUsageLow = 5;
	private int mediumUsageHigh = 10;
	
	private boolean lock = true;

	SharedPreferences settings;
	
	public Preferences(SharedPreferences _settings){
		settings = _settings;
	}
	/**
	* Store static param into preferences file
	*/
	public void storeParamToFile( ){

		SharedPreferences.Editor editor = settings.edit();

		if ( boxSize != -1 ) 
			editor.putInt(PREF_BOX_SIZE, boxSize);
		if ( boxPrice != -1f ) 
			editor.putFloat(PREF_BOX_PRICE, boxPrice);
		if ( cigarettePrice != -1f ) 
			editor.putFloat(PREF_CIGARETTE_PRICE, cigarettePrice);

		if ( screenWidth != -1 ) 
			editor.putInt(PREF_SCREEN_WIDTH, screenWidth);
		if ( screenHeight != -1 ) 
			editor.putInt(PREF_SCREEN_HEIGHT, screenHeight);

		if ( mediumUsageLow != -1 )
			editor.putInt(MEDIUM_USAGE_LOW, mediumUsageLow);
		if ( mediumUsageHigh != -1 )
			editor.putInt(MEDIUM_USAGE_HIGH, mediumUsageHigh);

		editor.putBoolean(PREF_TEST_UNLOCK, lock);

		editor.commit();
	}

	public void storeWidgetsIds(int[] widgetsIds){
		Log.d(LOG_TAG, "storeWidgetsIds()");

		SharedPreferences.Editor editor = settings.edit();
		
		String allIds = "";
		for ( int id : widgetsIds )
			allIds += id+"|";
			
		editor.putString(WIDGET_ID_TAB, allIds);
		editor.commit();
	}
	
	public void addWidgetId(int id){
		Log.d(LOG_TAG, "addWidgetId()");

		int[] tabWidgetsIds = this.retrieveWidgetsIds();
		int[] newTabWidgetsIds = new int[tabWidgetsIds.length+1];
		
		for (int i = 0 ; i < tabWidgetsIds.length ; i++ )
			newTabWidgetsIds[i] = tabWidgetsIds[i];

		newTabWidgetsIds[newTabWidgetsIds.length-1] = id;
		
		this.storeWidgetsIds(newTabWidgetsIds);
	}
	
	public int[] retrieveWidgetsIds(){
		Log.d(LOG_TAG, "retrieveWidgetsIds()");

		int[] widgetsIds = new int[0];
		
		try {
			
			String allWidgetsIds = settings.getString(WIDGET_ID_TAB, "");
			
			String[] tabWidgets = allWidgetsIds.split("|");
			widgetsIds = new int[tabWidgets.length];
			
			for (int i = 0 ; i < tabWidgets.length ; i++ )
				widgetsIds[i] = Integer.parseInt(tabWidgets[i]);
			
		} catch(Exception e){
			e.printStackTrace();
		}
		
		return widgetsIds;
	}
	
	/**
	* Read static param from preferences file
	*/
	public void readParamFromFile(){
		Log.d(LOG_TAG, "readParamFromFile()");
		try {

			boxSize  = settings.getInt(PREF_BOX_SIZE, -1);
			boxPrice = settings.getFloat(PREF_BOX_PRICE, -1f);
			cigarettePrice = settings.getFloat(PREF_CIGARETTE_PRICE, 0);
			lock = settings.getBoolean(PREF_TEST_UNLOCK, true);

			mediumUsageLow = settings.getInt(MEDIUM_USAGE_LOW, 5);
			mediumUsageHigh = settings.getInt(MEDIUM_USAGE_HIGH, 10);
					
		} catch(Exception e){
			e.printStackTrace();
			boxSize = -1;
			boxPrice = -1f;
			cigarettePrice = 0;
			lock = true;
		}
	}
	
	/**
	* Read static param from preferences file
	*/
	public void readExtraParamFromFile(){
		Log.d(LOG_TAG, "readExtraParamFromFile()");
		
		readParamFromFile();
		try {
			screenWidth  = settings.getInt(PREF_SCREEN_WIDTH, -1);
			screenHeight = settings.getInt(PREF_SCREEN_HEIGHT, -1);

		} catch(Exception e){
			e.printStackTrace();
			screenWidth = -1;
			screenHeight = -1;
		}
	}
	
	public void setDailyCount(int count){
		SharedPreferences.Editor editor = settings.edit();

		editor.putInt(DATA_DAILY_COUNT, count);
		 
		editor.commit();
		
	}
	
	public int getDailyCount(){
		int total = 0;
		
		try {

			total  = settings.getInt(DATA_DAILY_COUNT, -1);

		} catch(Exception e){
			e.printStackTrace();
			total = -1;
		}
		
		return total; 
	}
	
	
	public int getBoxSize() {
		return boxSize;
	}
	public void setBoxSize(int _boxSize) {
		this.boxSize = _boxSize;
	}
	public float getBoxPrice() {
		return boxPrice;
	}
	public void setBoxPrice(float _boxPrice) {
		this.boxPrice = _boxPrice;
	}
	public float getCigarettePrice() {
		return cigarettePrice;
	}
	public void setCigarettePrice(float cigarettePrice) {
		this.cigarettePrice = cigarettePrice;
	}
	public int getScreenWidth() {
		return screenWidth;
	}
	public void setScreenWidth(int screenWidth) {
		this.screenWidth = screenWidth;
	}
	public int getScreenHeight() {
		return screenHeight;
	}
	public void setScreenHeight(int screenHeight) {
		this.screenHeight = screenHeight;
	}
	
	public void setLock(boolean _lock){
		lock = _lock;
	}

	public boolean getLock(){
		return lock;
	}
	public int getMediumUsageLow() {
		return mediumUsageLow;
	}
	public void setMediumUsageLow(int mediumUsageLow) {
		this.mediumUsageLow = mediumUsageLow;
	}
	public int getMediumUsageHigh() {
		return mediumUsageHigh;
	}
	public void setMediumUsageHigh(int mediumUsageHigh) {
		this.mediumUsageHigh = mediumUsageHigh;
	}

	
	
}
